<?php
/**
 * Created by PhpStorm.
 * User: 快定
 * Date: 2022/1/5
 * Time: 15:12
 */

use Pheanstalk\Pheanstalk;
require "vendor/autoload.php";


$pea = Pheanstalk::create('10.0.0.200',11300,20);
while (true) {
    $job = $pea->watchOnly('default')->reserve();
    $sql = $job->getData();
    $data = json_decode($sql,true);
    $res = execSql($data);
    if ($res) {
        $pea->watchOnly('default')->delete($job);
        echo "执行成功!";
    } else {
        $pea->watchOnly('default')->bury($job);
    }
    usleep(500000);
}

function execSql($params)
{
    list($sql, $data) = [$params['sql'],$params['data']];
    $dbms = 'mysql';
    $host = "10.0.0.200";
    $dbName = "zfb";
    $dsn = "$dbms:host=$host;dbname=$dbName";
    try {
        $pdo = new PDO($dsn, 'wyq', 'WYQGG123@mysql');
        $sth = $pdo->prepare($sql);
        $sth->execute($data);
        return true;
    } catch (PDOException $e) {
        echo $e->getMessage();
        return false;
    }
}
